﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web.Routing;
using Kevin.Shop.Business.Service.Entity.Helper;
using RiderShop.Mobile.Common.Helper;

namespace RiderShop.Mobile.Common.Component
{
    public class ThemeController : Controller
    {
        protected override PartialViewResult PartialView(string viewName, object model)
        {
            return base.PartialView(viewName, model);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="theme"></param>
        /// <param name="controller"></param>
        /// <param name="view"></param>
        /// <returns></returns>
        protected PartialViewResult PartialView(string theme, string controller, string view)
        {
            string viewPath = GetViewPath(theme, controller, view);
            return base.PartialView(viewPath);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="theme"></param>
        /// <param name="controller"></param>
        /// <param name="view"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        protected PartialViewResult PartialView(string theme, string controller, string view, object model)
        {
            string viewPath = GetViewPath(theme, controller, view);
            return base.PartialView(viewPath, model);
        }

        protected override void Execute(RequestContext requestContext)
        {
            base.Execute(requestContext);
        }

        private static string[] pathTemplate = new string[]
        { 
            "~/Themes/{0}/Views/{1}/{2}.cshtml",
            "~/Themes/{0}/Views/Shared/{1}.cshtml"
        };

        private string GetViewPath(string theme, string controller, string view)
        {
            string viewPath = CacheViewsPath.GetViewPath(theme, controller, view);
            if (string.IsNullOrEmpty(viewPath))
            {
                viewPath = string.Format(pathTemplate[0], theme, controller, view);

                if (!System.IO.File.Exists(Server.MapPath(viewPath)))
                {
                    viewPath = string.Format(pathTemplate[0], "Default", controller, view);

                    if (!System.IO.File.Exists(Server.MapPath(viewPath)))
                    {
                        viewPath = string.Format("~/Views/{0}/{1}.cshtml", controller, view);
                    }
                }
                CacheViewsPath.SetViewPath(theme, controller, view, viewPath);
            }
            return viewPath;
        }

        private string GetLayoutPath(string theme, string master)
        {
            if (string.IsNullOrWhiteSpace(master))
            {
                return string.Empty;
            }

            string viewPath = CacheViewsPath.GetViewPath(theme, "Layout", master);
            if (string.IsNullOrEmpty(viewPath))
            {
                viewPath = string.Format(pathTemplate[1], theme, master);

                if (!System.IO.File.Exists(Server.MapPath(viewPath)))
                {
                    viewPath = string.Format(pathTemplate[1], "Default", master);

                    if (!System.IO.File.Exists(Server.MapPath(viewPath)))
                    {
                        viewPath = string.Format("~/Views/Shared/{0}.cshtml", master);

                        if (!System.IO.File.Exists(Server.MapPath(viewPath)))
                        {
                            viewPath = "_Layout";
                            if (!System.IO.File.Exists(Server.MapPath("~/Views/Shared/_Layout.cshtml")))
                            {
                                viewPath = "";
                            }
                        }
                    }
                }
                CacheViewsPath.SetViewPath(theme, "Layout", master, viewPath);
            }
            return viewPath;
        }

        private static ThemeController themeController = new ThemeController();

        public ViewResult View(string theme, string controller, string view, string master, object model)
        {
            string viewPath = GetViewPath(theme, controller, view);
            string masterPath = GetLayoutPath(theme, master);

            if (string.IsNullOrEmpty(masterPath))
            {
                return themeController.View(viewPath, model);
            }
            else
            {
                return themeController.View(viewPath, masterPath, model);
            }
        }

        public ViewResult View(string theme, string controller, string view, string master)
        {
            string viewPath = GetViewPath(theme, controller, view);
            string masterPath = GetLayoutPath(theme, master);
            if (string.IsNullOrEmpty(masterPath))
            {
                return themeController.View(viewPath);
            }
            else
            {
                return themeController.View(viewPath, masterPath);
            }
        }
    }
}
